package com.i9he.ping.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class IPStatus implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public IPStatus(String ip) {
		this.ip = ip;
	}

	private final String ip;

	private Date brokenTime;
	
	private Date delayTime;

	private Date reconnectTime;

	private int successCount;

	private int errorCount;
	
	private boolean delay; //是否延迟

	private boolean broken; //是否 
	
	private Object tag;

	public String getIp() {
		return ip;
	}

	public Date getBrokenTime() {
		return brokenTime;
	}
	
	public Date getDelayTime(){
		return delayTime;
	}

	public int getSuccessCount() {
		return successCount;
	}

	public void setSuccessCount(int successCount) {
		this.successCount = successCount;
	}

	public int getErrorCount() {
		return errorCount;
	}

	public void setErrorCount(int errorCount) {
		this.errorCount = errorCount;
	}

	public boolean isBroken() {
		return broken;
	}

	public void setBroken(boolean broken) {
		if (broken) {
			this.brokenTime = new Date();
		} else {
			this.reconnectTime = new Date();
		}
		this.broken = broken;
	}
	
	public boolean isDelay() {
		return delay;
	}

	public void setDelay(boolean delay) {
		if(delay){
			this.delayTime = new Date();
		}
		this.delay = delay;
	}

	private String getDiffTime(Date time1, Date time2) {
		int diff = (int)(time2.getTime() - time1.getTime()) / 1000; // 转换为秒单位
		StringBuilder sb = new StringBuilder();
		
		int hour = diff / 60 / 60;
		int minute = (diff / 60) % 60;
		int second = diff % 60;
		
		if (hour > 0) {
			sb.append(hour + "小时");
		}
		if (minute > 0) {
			sb.append(minute + "分");
		}
		if (second > 0) {
			sb.append(second + "秒");
		}
		return sb.toString();
	}

	public String getBrokenText() {
		String strTime = "";
		String diffTime = "";
		if (brokenTime != null) {
			strTime = dateFormat.format(brokenTime);
			diffTime = getDiffTime(brokenTime, new Date());
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("<tr>");
		sb.append(String.format("<td>%s</td>", ip));
		sb.append(String.format("<td>%s</td>", strTime));
		sb.append(String.format("<td>%s</td>", diffTime));
		sb.append("</tr>");
		
		return sb.toString();
	}

	public String getDelayText() {
		String strTime = "";
		if (delayTime != null) {
			strTime = dateFormat.format(delayTime);
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("<tr>");
		sb.append(String.format("<td>%s</td>", ip));
		sb.append(String.format("<td>%s</td>", strTime));
		sb.append("</tr>");
		
		return sb.toString();
	}
	
	public String getReconnectText() {
		String strTime = "";
		String strTime2 = "";
		String diffTime = "";
		if (brokenTime != null) {
			strTime = dateFormat.format(brokenTime);
		}
		if (reconnectTime != null) {
			strTime2 = dateFormat.format(reconnectTime);
		}
		if (brokenTime != null && reconnectTime != null) {
			diffTime = getDiffTime(brokenTime, reconnectTime);
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append("<tr>");
		sb.append(String.format("<td>%s</td>", ip));
		sb.append(String.format("<td>%s</td>", strTime));
		sb.append(String.format("<td>%s</td>", strTime2));
		sb.append(String.format("<td>%s</td>", diffTime));
		sb.append("</tr>");
		
		return sb.toString();
	}

	public Object getTag() {
		return tag;
	}

	public void setTag(Object tag) {
		this.tag = tag;
	}

	@Override
	public String toString() {
		return "IPStatus [ip=" + ip + ", brokenTime=" + brokenTime
				+ ", reconnectTime=" + reconnectTime + ", successCount="
				+ successCount + ", errorCount=" + errorCount + ", broken="
				+ broken + ", tag=" + tag + "]";
	}
	
	/*@Override
	public String toString() {
		return "IP: " + ip;
	}*/
	
}
